package com.net.io.netty.decode.http;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpContentDecompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;

/**
 * @author zhangtengfei
 * @date 2022/2/16 14:22
 */
public class HttpClient {
    public static final String HOST = "127.0.0.1";
    private static final boolean SSL = false;

    public void connect(String host, int port) throws Exception {
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(workerGroup);
            b.channel(NioSocketChannel.class);
            b.option(ChannelOption.SO_KEEPALIVE, true);
            b.handler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(SocketChannel ch) throws Exception {
                    /**
                     * 聚合 HTTP 消息
                     */
                    ch.pipeline().addLast(new HttpClientCodec());
                    // 聚合http为一个完整的报文
                    // 将最大的消息大小为 10MB 的 HttpObjectAggregator 添加到 ChannelPipeline
                    ch.pipeline().addLast("aggregator", new HttpObjectAggregator(10 * 1024 * 1024));
                    /**
                     * 解压缩
                     */
                    ch.pipeline().addLast("decompressor", new HttpContentDecompressor());
//                    ch.pipeline().addLast(new HttpClientInboundHandler());
                }
            });

            // Start the client.
            ChannelFuture f = b.connect(host, port).sync();
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        HttpClient client = new HttpClient();
        client.connect("127.0.0.1", HttpServer.port);
    }
}

